package com.jp.knowledge.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import com.jp.knowledge.fragment.CommNewsFragment;
import com.jp.knowledge.model.HeadLine;

import java.util.List;

/**
 * Created by jiayen on 16/9/27.
 */
public class FragMentAdapter extends FragmentStatePagerAdapter {

    private List<Fragment>  fragments;
    private List<HeadLine>  headLines;

    public FragMentAdapter(FragmentManager fm,List<Fragment> fragments) {
        super(fm);
        this.fragments=fragments;
    }
    public void updateFragments(List<Fragment> fragments, List<HeadLine> headLines){
        this.fragments=fragments;
        this.headLines=headLines;
        notifyDataSetChanged();
    }



    @Override
    public Fragment getItem(int position) {
        return fragments==null?null:fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments==null?0:fragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return  POSITION_NONE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        if(fragment instanceof CommNewsFragment && headLines != null && headLines.size() > 0){
            ((CommNewsFragment)fragment).newInstance(headLines.get(position).getTypeId());
        }
        return super.instantiateItem(container, position);
    }

//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeViewAt(position);
//    }

}